package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.omg.CORBA.FREE_MEM;

import controller.ClasserTexte;
import metier.FiltreMetier;

public class JClasser extends JFrame{
	
	JCheckBox accent1,accent2,adj1,adj2,l1,l2,norma1,norma2,lemme1,lemme2,min1,min2;
	JComboBox adjPossessif1, adjPossessif2,pFrequent1,pFrequent2,mFrequent1,mFrequent2;
	JTextField longueur1,longueur2;
	JLabel path,Jfiltre,Jfiltre2;
	String fichierClasser;
	JButton parcourir,valider;
	List<String> lAdj1,lAdj2,lPFrequent1,lPFrequent2,lMFrequent1,lMFrequent2;
	
	public JClasser() {
		super();
		init();
		setLayout(new GridLayout(1,2));
		add(leftPanel());
		add(rightPanel());
		
		setVisible(true);
	}
	
	public void init() {
		setTitle("Fouille De Textes - Classement");
	    setSize(new Dimension(800, 300));
	    setLocationRelativeTo(null);
	}
	
	public JPanel leftPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(10,1));
		JPanel tmp = new JPanel();
		tmp.setLayout(new GridLayout(1,2));
		path = new JLabel("/path");
		tmp.add(path);
		parcourir = new JButton("parcourir");
		parcourir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e1) {
				JFileChooser openFile = new JFileChooser();
                openFile.showOpenDialog(null);

                try {
                    // Open an input stream
                    Scanner reader = new Scanner(openFile.getSelectedFile());
                    if(reader != null) {
                        fichierClasser = openFile.getSelectedFile().getAbsolutePath();
                        String separatorPath = File.separator;
                		String[] tab = fichierClasser.split(Pattern
                				.quote(separatorPath));
                        path.setText(tab[tab.length -1]);   
                    }
                }
                catch(NullPointerException e){} 
                catch (FileNotFoundException e) {}
				
			}
		});
		tmp.add(parcourir);
		res.add(tmp);
		//ligne accent
		accent1 = new JCheckBox();
		res.add(panelAccent(accent1));
		//ligne normalisation
		norma1 = new JCheckBox();
		res.add(panelNormalisation(norma1));
		//ligne lemmatisation
		lemme1 = new JCheckBox();
		lemme1.setSelected(true);
		res.add(panelLemmatisation(lemme1));
		//ligne minuscule
		min1 = new JCheckBox();
		res.add(panelMinuscule(min1));
		//ligne adj. possessif
		adjPossessif1 = new JComboBox();
		adj1 = new JCheckBox();
		lAdj1 = new ArrayList<String>();
		addFilesAdj("filtres/adjectifsPossessifs", adjPossessif1, lAdj1);
		res.add(panelAdjPossessif(adjPossessif1, adj1));
		//ligne longueur mot
		longueur1 = new JTextField();
		l1 = new JCheckBox();
		res.add(panelLongueurMot(longueur1,l1));
		//ligne plus fréquent
		pFrequent1 = new JComboBox();
		lPFrequent1 = new ArrayList<String>();
		addFilesAdj("filtres/plusFrequent", pFrequent1, lPFrequent1);
		res.add(panelPlusFrequent(pFrequent1));
		//ligne plus fréquent
		mFrequent1 = new JComboBox();
		lMFrequent1 = new ArrayList<String>();
		addFilesAdj("filtres/moinsFrequent", mFrequent1, lMFrequent1);
		res.add(panelMoinsFrequent(mFrequent1));
		//résultat
		JPanel tmp2 = new JPanel();
		tmp2.setLayout(new GridLayout(1,2));
		tmp2.add(new JLabel("résultat : "));
		Jfiltre = new JLabel("");
		tmp2.add(Jfiltre);
		res.add(tmp2);
		
		
		
		
		return res;
	}
	
	public JPanel rightPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(10,1));
		valider = new JButton("valider");
		valider.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int intLongueur1 = 0;
				String adjectifs1 = "";
				if(l1.isSelected()){
					try {
						intLongueur1 = Integer.parseInt(longueur1.getText());
					} catch(NumberFormatException nfe) {
					     // pas un entier
					}
				}
				if(adj1.isSelected()){
					adjectifs1 = adjPossessif1.getSelectedItem().toString();
				}
				FiltreMetier filtre = new FiltreMetier(accent1.isSelected(), norma1.isSelected(), lemme1.isSelected(), min1.isSelected(), adjectifs1,lPFrequent1.get(pFrequent1.getSelectedIndex()),lMFrequent1.get(mFrequent1.getSelectedIndex()),intLongueur1 );
				
				int intLongueur2 = 0;
				String adjectifs2 = "";
				if(l2.isSelected()){
					try {
						intLongueur2 = Integer.parseInt(longueur2.getText());
					} catch(NumberFormatException nfe) {
					     // pas un entier
					}
				}
				if(adj2.isSelected()){
					adjectifs2 = adjPossessif2.getSelectedItem().toString();
				}
				FiltreMetier filtre2 = new FiltreMetier(accent2.isSelected(), norma2.isSelected(), lemme2.isSelected(), min2.isSelected(), adjectifs2,lPFrequent2.get(pFrequent2.getSelectedIndex()),lMFrequent2.get(mFrequent2.getSelectedIndex()),intLongueur2 );
				if(fichierClasser!= null){
					Jfiltre.setText("");
					Jfiltre.setForeground(Color.black);
					Jfiltre2.setText("");
					Jfiltre2.setForeground(Color.black);
					String res = ClasserTexte.classerTexteController(filtre, filtre2, fichierClasser);
					switch(res){
					case("un"):
						Jfiltre.setText("Meilleur classement");
						Jfiltre.setForeground(Color.green);
						break;
					case("deux"):
						Jfiltre2.setText("Meilleur classement");
						Jfiltre2.setForeground(Color.green);
						break;
					default:
						Jfiltre.setText("Similaire");
						Jfiltre.setForeground(Color.orange);
						Jfiltre2.setText("Similaire");
						Jfiltre2.setForeground(Color.orange);
						break;
					}
				}
			}
		});
		res.add(valider);
		//ligne accent
		accent2 = new JCheckBox();
		res.add(panelAccent(accent2));
		//ligne normalisation
		norma2 = new JCheckBox();
		res.add(panelNormalisation(norma2));
		//ligne lemmatisation
		lemme2 = new JCheckBox();
		lemme2.setSelected(true);
		res.add(panelLemmatisation(lemme2));
		//ligne minuscule
		min2 = new JCheckBox();
		res.add(panelMinuscule(min2));
		//ligne adj. possessif
		adjPossessif2 = new JComboBox();
		adj2 = new JCheckBox();
		lAdj2 = new ArrayList<String>();
		addFilesAdj("filtres/adjectifsPossessifs", adjPossessif2, lAdj2);
		res.add(panelAdjPossessif(adjPossessif2, adj2));
		//ligne longueur mot
		longueur2 = new JTextField();
		l2 = new JCheckBox();
		res.add(panelLongueurMot(longueur2,l2));
		//ligne plus fréquent
		pFrequent2 = new JComboBox();
		lPFrequent2 = new ArrayList<String>();
		addFilesAdj("filtres/plusFrequent", pFrequent2, lPFrequent2);
		res.add(panelPlusFrequent(pFrequent2));
		//ligne moins fréquent
		mFrequent2 = new JComboBox();
		lMFrequent2 = new ArrayList<String>();
		addFilesAdj("filtres/moinsFrequent", mFrequent2, lMFrequent2);
		res.add(panelMoinsFrequent(mFrequent2));
		//résultat
		//résultat
		JPanel tmp2 = new JPanel();
		tmp2.setLayout(new GridLayout(1,2));
		tmp2.add(new JLabel("résultat : "));
		Jfiltre2 = new JLabel("");
		tmp2.add(Jfiltre2);
		res.add(tmp2);
		
		
		
		
		
		return res;
	}
	
	public JPanel panelAccent(JCheckBox box) {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("Accent"));
		res.add(box);
		
		return res;
	}
	
	public JPanel panelAdjPossessif(JComboBox combo,JCheckBox box){
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,3));
		res.add(new JLabel("adj. possessif"));
		JPanel tmp = new JPanel();
		tmp.setLayout(new GridLayout(1,2));
		tmp.add(combo);
		tmp.add(box);
		res.add(tmp);
		
		return res;
	}
	
	public JPanel panelLongueurMot(JTextField combo,JCheckBox box){
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,3));
		res.add(new JLabel("longueur"));
		JPanel tmp = new JPanel();
		tmp.setLayout(new GridLayout(1,2));
		tmp.add(combo);
		tmp.add(box);
		res.add(tmp);
		
		return res;
	}
	
	public JPanel panelNormalisation(JCheckBox box) {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("Normalisation"));
		res.add(box);
		
		return res;
	}
	
	public JPanel panelLemmatisation(JCheckBox box){
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("Lemmatisation"));
		res.add(box);
		
		return res;
	}
	
	public JPanel panelPlusFrequent(JComboBox combo){
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("+ Frequent"));
		res.add(combo);
		
		return res;
	}
	
	public JPanel panelMoinsFrequent(JComboBox combo){
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("- Frequent"));
		res.add(combo);
		
		return res;
	}
	
	public JPanel panelMinuscule(JCheckBox box) {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,2));
		res.add(new JLabel("Minuscule"));
		res.add(box);
		
		return res;
	}
	
	public void addFilesAdj(String path,JComboBox combo, List<String> listPath){
		File folder = new File(path);
		for(File f : folder.listFiles()){
			if(!f.getName().equals(".DS_Store") && f.isFile()){
				String separatorPath = File.separator;
        		String[] tab = f.getAbsolutePath().toString().split(Pattern
        				.quote(separatorPath));
                combo.addItem(tab[tab.length-1]);
                listPath.add(f.getAbsolutePath());
            }
		}
	}
	
	

}
